package com.code.translate.utils;

import com.alibaba.fastjson.JSON;
import org.springframework.util.Base64Utils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Security;

public class AESUtils {

    /**
     * 加密操作
     *
     * @param content        待加密内容
     * @param encodingFormat 编码方式
     * @param secretKey      密码
     * @param ivParameter    偏移量
     * @return
     * @throws Exception
     */
    public static String encrypt(String content, String encodingFormat, byte[] secretKey, byte[] ivParameter) {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec skeySpec = new SecretKeySpec(secretKey, "AES");
            IvParameterSpec iv = new IvParameterSpec(ivParameter);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            byte[] encrypted = cipher.doFinal(content.getBytes(encodingFormat));
            return Base64Utils.encodeToString(encrypted);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解密操作
     *
     * @param content        待解密内容
     * @param encodingFormat 编码方式
     * @param secretKey      密码
     * @param ivParameter    偏移量
     * @return
     * @throws Exception
     */
    public static String decrypt(String content, String encodingFormat, byte[] secretKey, byte[] ivParameter) {
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(secretKey, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(ivParameter);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

            byte[] original = cipher.doFinal(content.getBytes());
            String originalString = new String(original, encodingFormat);
            return originalString;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
        String key = "ydsecret://query/key/B*RGygVywfNBwpmBaZg*WT7SIOUP2T0C9WHMZN39j^DAdaZhAnxvGcCY6VYFwnHl";
        String iv = "ydsecret://query/iv/C@lZe2YzHtZ2CYgaXKSVfsb7Y4QWHjITPPZ0nQp87fBeJ!Iv6v^6fvi2WN@bYpJ4";
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

        MessageDigest messageDigest = MessageDigest.getInstance("md5");
        messageDigest.update(key.getBytes());
        byte[] ss = messageDigest.digest();
        int[] ii = new int[ss.length];
        for (int i = 0; i < ss.length; i++) {
            ii[i] = Byte.toUnsignedInt(ss[i]);
        }

        messageDigest = MessageDigest.getInstance("md5");
        messageDigest.update(iv.getBytes());
        byte[] ss2 = messageDigest.digest();
        int[] ii2 = new int[ss2.length];
        for (int i = 0; i < ss2.length; i++) {
            ii2[i] = Byte.toUnsignedInt(ss2[i]);
        }
        System.out.println(JSON.toJSONString(ii));
        System.out.println(JSON.toJSONString(ii2));

        String text = "这是一本书";
        text = "{\"code\":0,\"dictResult\":{},\"translateResult\":[[{\"tgt\":\"这是一本书\",\"src\":\"This is book\",\"tgtPronounce\":\"zhè shì yī bĕnshū\"}]],\"type\":\"en2zh-CHS\"}";

        String tt = "Z21kD9ZK1ke6ugku2ccWuwRmpItPkRr5XcmzOgAKD0GcaHTZL9kyNKkN2aYY6yiOv5GGcWlmtX250PEYCFNezyR9aEGVxdWdl8p75CSgTUikLgSpQ9VfIJzqAk2JFKEr0UfH8BOgmHBG_SPOlgi186HdLfVtrI7gp77L1To-zIv46e_kCN-0MUvEUzsXYmoq8Ze2S-xFAMLbucnvmItuVg==";
        tt = "Z21kD9ZK1ke6ugku2ccWuwRmpItPkRr5XcmzOgAKD0GcaHTZL9kyNKkN2aYY6yiOv5GGcWlmtX250PEYCFNezyR9aEGVxdWdl8p75CSgTUikLgSpQ9VfIJzqAk2JFKEr0UfH8BOgmHBG_SPOlgi186HdLfVtrI7gp77L1To-zIv46e_kCN-0MUvEUzsXYmoq8Ze2S-xFAMLbucnvmItuVg==";


        tt = "WjIxa0Q5Wksxa2U2dWdrdTJjY1d1d1JtcEl0UGtScjVYY216T2dBS0QwR2NhSFRaTDlreU5La04yYVlZNnlpT3Y1R0djV2xtdFgyNTBQRVlDRk5lenlSOWFFR1Z4ZFdkbDhwNzVDU2dUVWlrTGdTcFE5VmZJSnpxQWsySkZLRXIwVWZIOEJPZ21IQkdfU1BPbGdpMTg2SGRMZlZ0ckk3Z3A3N0wxVG8tekl2NDZlX2tDTi0wTVV2RVV6c1hZbW9xOFplMlMteEZBTUxidWNudm1JdHVWZz09";
//        System.out.println(Base64.getDecoder().decode());
//        System.out.println(Base64Utils.decodeFromString(tt));
//        System.out.println(encrypt(text, "utf8", ss, ss2));
//        System.out.println(decrypt(tt, "utf8", ss, ss2));
        System.out.println(Base64Utils.encodeToString("1".getBytes()));
    }
}
